package com.qhg.saas_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.empty.Result;
import com.qhg.saas_server.mapper.UpLoadMapper;
import com.qhg.saas_server.pojo.Upload;
import com.qhg.saas_server.pojo.dto.UpLoadDto;
import com.qhg.saas_server.service.UpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UpLoadServiceImpl implements UpLoadService {

    @Autowired
    UpLoadMapper upLoadMapper;

    /**
     * 条件查询上传信息
     * @param upLoadDto
     * @return
     */
    @Override
    public Result getUploadByCondition(UpLoadDto upLoadDto) {


        String sortType = "ASC";
        //确定排序规则
        Integer sortRule = upLoadDto.getSortRule();
        if(!Objects.isNull(sortRule)){
            sortType =    sortRule == 1 ? "DESC" : "ASC";
        }

        PageHelper.startPage(upLoadDto.getPageNum(), upLoadDto.getPageSize(),"`upload`.uploadTime  "+sortType);
        List<UpLoadDto> upLoadDtoList= upLoadMapper.getUploadByCondition(upLoadDto);
        PageInfo<UpLoadDto> info = new PageInfo<>(upLoadDtoList);
        return Result.success(info);

    }

    /**
     * 增加上传信息
     * @param upload
     */
    @Override
    public void addUpload(Upload upload) {

        upLoadMapper.insert(upload);
    }

    /**
     * 根据uploadId删除上传信息
     * @param uploadId
     */
    @Override
    public void deleteUpload(Integer uploadId) {

        upLoadMapper.delete(uploadId);
    }
}
